Why is there never any controversy regarding the switch statement? [closed]

Posted by Nick Rosencrantz on Programmers See other posts from Programmers or by Nick Rosencrantz
Published on 2012-12-03T03:55:34Z Indexed on 2012/12/03 5:49 UTC
Read the original article Hit count: 451

We all know that the gotostatement should only be used on very rare occasions if at all. It has been discouraged to use the goto statement countless places countless times. But why it there never anything like that about the switch statement? I can understand the position that the switch statement should always be avoided since anything with switch can always be expressed by if...else... which is also more readable and the syntax of the switch statement if difficult to remember. Do you agree? What are the arguments in favor of keeping the 'switch` statement? It can also be difficult to use if what you're testing changes from say an integer to an object, then C++ or Java won't be able to perform the switch and neither C can perform switch on something like a struct or a union. And the technique of fall-through is so very rarely used that I wonder why it was never presented any regret of having switch at all? The only place I know where it is best practice is GUI code and even that switch is probably better coded in a more object-oriented way.

© Programmers or respective owner

Related posts about readability

Related posts about switch-statement